package cn.yeziji.forum.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.OffsetDateTime;
import java.time.ZoneId;
import java.time.temporal.ChronoUnit;
import java.util.Calendar;
import java.util.Date;
import java.util.concurrent.TimeUnit;

/**
 * 时间工具类
 *
 * @author gzkemays
 * @since 2021/12/23 0:03
 */
public class TimeUtils {
  public static String COMMON_SIMPLE_PATTERN = "yyyy-MM-dd HH:mm:ss";
  public static String COMMON_MINUTE_PATTERN = "yyyy-MM-dd HH:mm:ss";
  public static String TIME_PATTERN = "HH:mm:ss";
  public static String TIME_MINUTE_PATTERN = "HH:mm";
  public static SimpleDateFormat COMMON_SDF = new SimpleDateFormat(COMMON_SIMPLE_PATTERN);
  public static String[] WEEK_DAYS = {"星期一", "星期二", "星期三", "星期四", "星期五", "星期六", "星期日"};

  public static long getTime(Integer time, TimeUnit timeUnit) {
    long t;
    switch (timeUnit) {
      case MINUTES:
        t = time * 60;
        break;
      case HOURS:
        t = time * 60 * 60;
        break;
      case DAYS:
        t = time * 60 * 60 * 24;
        break;
      default:
        t = time;
    }
    return t;
  }

  /**
   * 比较 time1 是否大于 time2
   *
   * @param time1 时间1
   * @param time2 时间2
   * @param format 对应时间格式
   * @return 比较结果
   */
  public static Boolean compareTime(String time1, String time2, String format) {
    SimpleDateFormat sdf = new SimpleDateFormat(format);
    try {
      Date parse = sdf.parse(time1);
      Date parse1 = sdf.parse(time2);
      return parse.getTime() > parse1.getTime();
    } catch (ParseException e) {
      e.printStackTrace();
    }
    return null;
  }

  /**
   * 获取 time1 与 time2 相差的天数
   *
   * @param time1 时间1
   * @param time2 时间2
   * @param format 格式化
   * @return 相差的天数
   */
  public static Long getDisparityDay(String time1, String time2, String format) {
    SimpleDateFormat sdf = new SimpleDateFormat(format);
    try {
      Date dateOne = sdf.parse(time1);
      Date dateTwo = sdf.parse(time2);
      long day = (dateOne.getTime() - dateTwo.getTime()) / (1000 * 60 * 60 * 24);
      return Math.abs(day);
    } catch (ParseException e) {
      e.printStackTrace();
    }
    return null;
  }

  /**
   * 允许时间戳与当前系统时间戳之间的波动值
   *
   * @param timestamp 时间戳
   * @param allowMax 波动值
   * @return 是否在其之内
   */
  public static boolean timestampBetweenFluctuations(long timestamp, long allowMax) {
    return System.currentTimeMillis() - timestamp < allowMax;
  }
  /**
   * 根据日期获取当天是周几
   *
   * @param datetime 日期
   * @return 周几
   */
  public static String dateToWeek(String datetime) {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    Calendar cal = Calendar.getInstance();
    Date date;
    try {
      date = sdf.parse(datetime);
      cal.setTime(date);
    } catch (ParseException e) {
      e.printStackTrace();
    }
    int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
    return WEEK_DAYS[w];
  }

  public static String dateToWeek(Date datetime) {
    Calendar cal = Calendar.getInstance();
    cal.setTime(datetime);
    int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
    return WEEK_DAYS[w];
  }

  public static String buildNoticeTime(Date startTime, Date endTime) {
    String noticeTime;
    Instant generateInstant = startTime.toInstant();
    Instant sendInstant = endTime.toInstant();
    ZoneId zoneId = ZoneId.systemDefault();
    // 构造 local date
    OffsetDateTime generateDate = generateInstant.atZone(zoneId).toOffsetDateTime();
    OffsetDateTime sendDate = sendInstant.atZone(zoneId).toOffsetDateTime();
    long dateBetween = ChronoUnit.SECONDS.between(generateDate, sendDate);
    String prefix = "秒前";
    if (dateBetween == 0) {
      dateBetween = 1;
    }
    if (dateBetween > 59) {
      dateBetween = ChronoUnit.MINUTES.between(generateDate, sendDate);
      prefix = "分钟前";
      if (dateBetween > 59) {
        dateBetween = ChronoUnit.HOURS.between(generateDate, sendDate);
        prefix = "小时前";
        if (dateBetween > 23) {
          dateBetween = ChronoUnit.DAYS.between(generateDate, sendDate);
          prefix = "天前";
          if (dateBetween > 365) {
            dateBetween = ChronoUnit.YEARS.between(generateDate, sendDate);
            prefix = "年前";
          }
        }
      }
    }
    noticeTime = dateBetween + prefix;
    return noticeTime;
  }

  public static void main(String[] args) {
    System.out.println(timestampBetweenFluctuations(123, (30 * 1000)));
  }
}
